import java.util.Scanner;
import java.util.Stack;

public class kuohaopeidui {
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<Character>();
        for(char c:s.toCharArray())
        {
            if(c=='(')
            {
                stack.push(')');
            }
            else if(c== '{')
            {
                stack.push('}');
            }
            else if(c=='[')
            {
                stack.push(']');
            }
            else if(stack.isEmpty() || c!=stack.pop())
            {
                return false;
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        Stack<Character> stack = new Stack<Character>();
        Scanner in =new Scanner(System.in);
        while (in.hasNext()) {
            int c=in.nextInt();
            for (int i = 0; i <c ; i++) {
                String a = in.next();
                boolean b = isValid(a);
                if(b==true)
                    System.out.println("True");
                if(b==false)
                    System.out.println("False");
            }
        }
    }
}
